﻿///////////////////////////////////////////////////////////////
// Projet serveur entreprise
// Authors: Pierre Corsini, Guillaume Blanc
// mars 2011
///////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;

using System.Text;
using DLL;
using System.Threading;

namespace InterfaceAdmin
{
    class Program
    {
        static void Main(string[] args)
        {
            AdministrateurService service = null;

            while (service == null)
            {
                Console.Write("User: ");
                String user = Console.ReadLine();
                Console.Write("Pwd: ");
                String pwd = Console.ReadLine();

                if (Securite.seConnecter(user, pwd, Securite.Droit.Admin) != null)
                    service = new AdministrateurService();
                else
                    Console.WriteLine("Mauvais identifiants");
            }

            Console.WriteLine("Vous étes connecté !");
            Console.WriteLine(Thread.CurrentPrincipal.Identity.AuthenticationType);

            String line;
            char[] delimiter = new char[] { '\t', ' ' };

            while (true)
            {
                Console.Write(">");

                if (equals(line = Console.ReadLine(), "quit"))
                    return;
                else
                    if (run(service, line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)))
                        Console.WriteLine("Done");
                    else
                        Console.WriteLine("Opération non effectuée !");
            }
        }

        static bool run(AdministrateurService service, string[] args)
        {
            if (args.Length == 0 || args[0] == "help")
            {
                Console.WriteLine("Commandes");

                Console.WriteLine("lister");
                Console.WriteLine("- client");
                Console.WriteLine("- client [idconseiller]");
                Console.WriteLine("- compte [idclient]");
                Console.WriteLine("- conseiller");
                Console.WriteLine("ajouter");
                Console.WriteLine("- client [id] [user] [pwd] [nom] [prenom]");
                Console.WriteLine("- conseiller [id] [user] [pwd] [nom] [prenom]");
                Console.WriteLine("affecter client [idclient] [idconseiller]");
                Console.WriteLine("supprimer");
                Console.WriteLine("- client [id]");
                Console.WriteLine("- compte [id]");
                Console.WriteLine("- conseiller [id]");
                return true;
            }


            try
            {
                switch (args[0])
                {
                    case "lister":
                        switch (args[1])
                        {
                            case "client":
                                if (args.Length > 2)
                                    foreach (Client c in service.listeClient(Int16.Parse(args[2]))) Console.WriteLine(c);
                                else
                                    foreach (Client c in service.listeClient()) Console.WriteLine(c);
                                return true;
                            case "compte":
                                foreach (Compte c in service.listeCompte(Int16.Parse(args[2]))) Console.WriteLine(c);
                                return true;
                            case "conseiller":
                                foreach (Conseiller c in service.listeConseiller()) Console.WriteLine(c);
                                return true;
                        }
                        break;

                    case "ajouter":
                        switch (args[1])
                        {
                            case "client":
                                return service.ajouterClient(new Client(new Utilisateur(Int32.Parse(args[2]), args[3], args[4], args[5], args[6])));

                            case "conseiller":
                                return service.ajouterConseiller(new Conseiller(new Utilisateur(Int32.Parse(args[2]), args[3], args[4], args[5], args[6])));
                        }
                        break;

                    case "affecter":
                        switch (args[1])
                        {
                            case "client":
                                return service.affecterClient(Int16.Parse(args[2]), Int16.Parse(args[3]));
                        }
                        break;

                    case "supprimer":
                        switch (args[1])
                        {
                            case "client":
                                return service.supprimerClient(Int16.Parse(args[2]));
                            case "compte":
                                return service.supprimerCompte(Int16.Parse(args[2]));
                            case "conseiller":
                                return service.supprimerConseiller(Int16.Parse(args[2]));
                        }
                        break;
                }

            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("Erreur, paramétre(s) manquant(s).");
            }
            catch (FormatException e)
            {
                Console.WriteLine("Erreur, paramétre(s) mal formaté(s).");
            }


            Console.WriteLine("Erreur, commande non reconnue.");
            return false;
        }

        static bool equals(String a, String b)
        {
            return a.Equals(b, StringComparison.OrdinalIgnoreCase);
        }
    }
}
